<template>
  <div class="app-container">
    <Search @reset="reset" @search="handleSearch">
      <div>
        <label>项目</label>
        <el-input v-model="searchParams.projectName" placeholder="请输入" clearable @change="handleSearch"></el-input>
      </div>
      <div>
        <label>测试选择</label>
        <el-select v-model="searchParams.test" placeholder="请选择" clearable filterable @change="handleSearch">
          <el-option v-for="item in options.test" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </div>
    </Search>
    <vxe-grid v-bind="gridConfig" class="sticky-header">
      <template #toolbar_buttons>
        <el-button icon="PlusIcon" @click="operateRef.open({ type: 'add' })">新增</el-button>
      </template>
      <template #operate="{ row }">
        <el-button link type="primary" @click="operateRef.open({ type: 'edit', id: row.id })">修改</el-button>
        <el-button link @click="operateRef.open({ type: 'detail', id: row.id })">详情</el-button>
        <el-popconfirm title="确定删除吗?" @confirm="handleDelete(row.id)">
          <template #reference>
            <el-button link type="danger">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
      <template #status="{ row }">
        <el-switch v-model="row.status" @change="updateStatus(row.id)" />
      </template>
      <template #pager>
        <vxe-pager
          v-model:current-page="searchParams.pageNum"
          v-model:page-size="searchParams.pageSize"
          :total="searchParams.total"
          @page-change="getList"
        >
        </vxe-pager>
      </template>
    </vxe-grid>
  </div>

  <OperateComponent :options="options" ref="operateRef" @refresh-list="getList"></OperateComponent>
</template>

<script lang="ts" setup>
import Search from "@/components/Search/index.vue"
import OperateComponent from "./operate.vue"
import { reactive, onMounted, ref } from "vue"
import { VxeGridProps } from "vxe-table"
import { ElMessage } from "element-plus"
import { getOrgTreeList, getSupplierDropDownList } from "@/api/public"
import { projectPage, changeStatus, delByIds } from "@/api/projectManage/project"
import { Options } from "./type"

defineOptions({
  name: "Project"
})

const operateRef = ref()
const searchParams = reactive(setDefaultSearchParams())
const options = reactive<Options>({
  test: [
    {
      label: "测试",
      value: "1"
    },
    {
      label: "测试2",
      value: "2"
    }
  ]
})
const gridConfig = reactive<VxeGridProps>({
  id: "ProjectList", // 表格唯一标识
  customConfig: {
    storage: true
  },
  toolbarConfig: {
    custom: true,
    slots: {
      buttons: "toolbar_buttons"
    }
  },
  columns: [
    { type: "seq", width: 50 },
    { title: "项目ID", field: "id" },
    { title: "项目名称", field: "projectName" },
    { title: "项目简称", field: "nickName" },
    { title: "经营模式", field: "businessModel_text" },
    { title: "项目周期", field: "projectPeriod_text" },
    { title: "所属组织", field: "orgName" },
    { title: "操作", field: "operate", slots: { default: "operate" }, showOverflow: false, width: 180 }
  ],
  data: []
})

function updateStatus(id: number) {
  changeStatus({ id })
    .then(() => {
      ElMessage.success("操作成功")
      getList()
    })
    .catch(() => {
      getList()
    })
}

function handleDelete(id: number) {
  delByIds({ ids: id }).then(() => {
    ElMessage.success("删除成功")
    getList()
  })
}

function handleSearch() {
  searchParams.pageNum = 1
  getList()
}

function reset() {
  Object.assign(searchParams, setDefaultSearchParams())
  handleSearch()
}

function setDefaultSearchParams() {
  return {
    projectName: '',
    test: '',
    pageNum: 1,
    pageSize: 10,
    total: 0
  }
}

function getList() {
  projectPage(searchParams).then((res: any) => {
    gridConfig.data = res.data.list
    searchParams.total = res.data.total
  })
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped></style>
